﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DFGK.WMS.Common.Web
{
    /// <summary>
    /// 根url
    /// </summary>
    public static class HttpHelper
    {
        /// <summary>
        /// 输送线URL
        /// </summary>
        private readonly static string lineUrl = "";

        /// <summary>
        /// wcsurl
        /// </summary>
        //private readonly static string wcsUrl = "http://yqxaapi.cscsit.com/";

        /// <summary>
        /// AgvUrl
        /// </summary>
        //private readonly static string agvUrl = "";

        /// <summary>
        /// post请求 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <returns></returns>

        public static string SendPostRequest(string apiName, object data)
        {
            try
            {
                string path = GetUrlByName(apiName);
                if (string.IsNullOrEmpty(path))
                {
                    throw new Exception("找不到相应的接口");
                }

                string url = CombineUrl(lineUrl, path);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";

                string jsonData = JsonConvert.SerializeObject(data);

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(jsonData);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine(ex.Message);
                return "系统错误";
            }
        }

        /// <summary>
        /// post请求 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <returns></returns>

        public static LineRequst SendPostRequestes(string apiName, object data)
        {

            try
            {
                string path = GetUrlByName(apiName);
                if (string.IsNullOrEmpty(path))
                {
                    throw new Exception("找不到相应的接口");
                }

                string url = CombineUrl(lineUrl, path);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";

                string jsonData = JsonConvert.SerializeObject(data);

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(jsonData);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string result = reader.ReadToEnd();
                        // 解析返回的结果
                        JObject json = JObject.Parse(result);
                        LineRequst line = new LineRequst();
                        line.returnStatus = (int)json["returnStatus"];
                        line.returnInfo = (string)json["returnInfo"];
                        line.msgTime = (string)json["msgTime"];
                        line.data = (string)json["data"];
                        return line;
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine(ex.Message);
                return new LineRequst() { returnStatus = 1, returnInfo = "系统错误", msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
            }
        }

        /// <summary>
        /// WCSpost请求 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <returns></returns>

        public static string WCSSendPostRequest(string path, object data)
        {
            try
            {
                string wcsUrl = "http://172.16.99.12:28086/";
                //string wcsUrl = "localhost/";
                string url = wcsUrl + path;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";

                string jsonData = JsonConvert.SerializeObject(data);

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(jsonData);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine(ex.Message);
                return "系统错误";
            }
        }

      
        /// <summary>
        /// Agvpost请求 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <returns></returns>

        public static string AgvSendPostRequest(string path, object data)
        {
            try
            {
                //string agvUrl = "localhost/";
                string agvUrl = "http://172.16.99.24:8080/";
                string url = agvUrl + path;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";

                string jsonData = JsonConvert.SerializeObject(data);

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(jsonData);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine(ex.Message);
                return "系统错误";
            }
        }


        /// <summary>
        /// 把根目录和接口目录拼接
        /// </summary>
        /// <param name="baseUrl"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private static string CombineUrl(string baseUrl, string path)
        {
            if (string.IsNullOrEmpty(baseUrl))
            {
                return path;
            }

            if (string.IsNullOrEmpty(path))
            {
                return baseUrl;
            }

            if (baseUrl.EndsWith("/") && path.StartsWith("/"))
            {
                return baseUrl + path.Substring(1);
            }

            if (!baseUrl.EndsWith("/") && !path.StartsWith("/"))
            {
                return baseUrl + "/" + path;
            }

            return baseUrl + path;
        }

        /// <summary>
        /// 根据接口名称获取相应url
        /// </summary>
        /// <param name="apiname"></param>
        /// <returns></returns>
        public static string GetUrlByName(string apiname)
        {
            string str = string.Empty;
            switch (apiname)
            {
                case "出库任务":
                    str = "";
                    break;
                case "入库拣选":
                    str = "";
                    break;
                case "获取巷道节点":
                    str = "";
                    break;

                default:
                    break;
            }
            return str;
        }
    }
}
